package com.pk.platform.business.core.dao.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.pk.platform.business.core.dao.ITreeMakerDao;

public class TreeMakerDaoImpl extends GenericDaoImpl implements ITreeMakerDao {

	/**
	 * <p>
	 * 说明:根据条件获得树(父节点下的所有子节点)
	 * </p>
	 * @param sql 查询树语句
	 * @param pv 参数
	 * @param field 必需要有7个信息  0:名称字段  1:自己ID字段  2:父节点ID字段  3:超链字段  4:图标字段  5:排序字段  6:字体样式
	 * @param isOpen 初始化时树是否展开
	 * @param hasCb 是否带有checkbox
	 * @param cbName checkbox名字
	 * @param cbClick 点击checkbox时的事件
	 * @return
	 */
	public String getTree(String table, String root, String[] field, boolean isOpen, boolean hasCb, String cbName, String cbClick)
	{
		return this.getTree(table, root, field, isOpen, hasCb, cbName, cbClick, null);
	}
	
	
	/**
	 * <p>
	 * 说明:根据条件获得树(带sql条件)
	 * </p>
	 * @param table
	 * @param root
	 * @param field
	 * @param isOpen
	 * @param hasCb
	 * @param cbName
	 * @param cbClick
	 * @param condition sql条件 例: and isshow = '1'
	 * @return
	 */
	public String getTree(String table, String root, String[] field, boolean isOpen, boolean hasCb, String cbName, String cbClick, String condition)
	{
		String result = this.makeTree(table, root, field, isOpen, hasCb, cbName, cbClick, condition);
		return result;
	}
	
	
	/**
	 * <p>
	 * 说明:异步树
	 * </p>
	 * @param table
	 * @param root
	 * @param field
	 * @param condition
	 * @param action
	 * @param resp
	 * @throws IOException
	 */
	public void getTree(String table, String root, String[] field, String condition, String action, boolean hasCb, String cbName, String cbClick, HttpServletResponse resp) throws IOException
	{
	    StringBuffer tree = new StringBuffer();
	    List<Map<String, Object>> list = this.getFirstLevelTree(table, root, field, condition);
        for(Map<String, Object> m : list)
        {
            //超链地址
            String url = "";
            if(field[3] != null && !"".equals(field[3]))
            {
                url = " url='" + m.get(field[3]) + "'";
            }
            //图标
            String img = "";
            if(field[4] != null && !"".equals(field[4]) && m.get(field[4]) != null && !"".equals(m.get(field[4])))
            {
                img = "<img src='" + m.get(field[4]) + "' />";
            }
            
            //复选框
            StringBuffer checkbox = new StringBuffer();
            if(hasCb)
            {
                String click = "";
                if(cbClick != null && !"".equals(cbClick))
                {
                    click = " onclick='" + cbClick + "($(this))'";
                }
                String name = "";
                if(cbName != null && !"".equals(cbName))
                {
                    name = " name='" + cbName + "'";
                }
                checkbox.append("<input value='").append(m.get(field[1])).append("' type='checkbox'").append(name).append(" id='").append(cbName).append(m.get(field[1])).append("' pid='").append(m.get(field[2])).append("'").append(click).append(" />");
            }
            
            tree.append("<li class='simpleTree' code='").append(m.get(field[1])).append("' id='li").append(m.get(field[1])).append("'").append(url).append("><span id='span").append(m.get(field[1])).append("'");
            if(field.length > 6) //如果还有字体样式
            {
                tree.append(" style='").append(m.get(field[6])).append("'");
            }
            tree.append(">").append(img).append(m.get(field[0])).append("</span>").append(checkbox);
            List<Map<String, Object>> treeList = this.getFirstLevelTree(table, m.get(field[1]).toString(), field, condition);
            if(treeList.size() != 0)
            {
                tree.append("<ul id='ul").append(m.get(field[1])).append("' class='ajax' ><li>{url:").append(action).append(m.get(field[1])).append("}</li></ul>");
            }
            tree.append("</li>");
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(tree.toString());
	}
	
	
	
	/**
	 * <p>
	 * 说明:根据条件组建树
	 * </p>
	 * @param allNode
	 * @param root
	 * @param field
	 * @param isOpen
	 * @param hasCb
	 * @param cbName
	 * @param cbClick
	 * @param condition
	 * @return
	 */
	private String makeTree(String table, String root, String[] field, boolean isOpen, boolean hasCb, String cbName, String cbClick, String condition)
	{
		StringBuffer result = new StringBuffer();
		List<Map<String, Object>> firstLevelTree = this.getFirstLevelTree(table, root, field, condition);
		if(firstLevelTree.size() > 0)
		{
			result.append("<ul id='ul" + root + "'>");
			for(Map<String, Object> m : firstLevelTree)
			{
				//复选框
				StringBuffer checkbox = new StringBuffer();
				if(hasCb)
				{
					String click = "";
					if(cbClick != null && !"".equals(cbClick))
					{
						click = " onclick='" + cbClick + "($(this))'";
					}
					String name = "";
					if(cbName != null && !"".equals(cbName))
					{
						name = " name='" + cbName + "'";
					}
					checkbox.append("<input value='").append(m.get(field[1])).append("' type='checkbox'").append(name).append(" id='").append(cbName).append(m.get(field[1])).append("' pid='").append(m.get(field[2])).append("'").append(click).append(" />");
				}
				
				//是否展开
				String open = "close";
				if(true == isOpen)
				{
					open = "open";
				}
				
				//超链地址
				String url = "";
				if(field[3] != null && !"".equals(field[3]))
				{
					url = " url='" + m.get(field[3]) + "'";
				}
				
				//图标
				String img = "";
				if(field[4] != null && !"".equals(field[4]) && m.get(field[4]) != null && !"".equals(m.get(field[4])))
				{
					img = "<img src='" + m.get(field[4]) + "' />";
				}
				
				//树节点
				result.append("<li").append(url).append(" id='li").append(m.get(field[1])).append("' class='folder-").append(open).append("' code='").append(m.get(field[1])).append("'><span id='span").append(m.get(field[1])).append("'");
				if(field.length > 6) //如果还有字体样式
	            {
				    result.append(" style='").append(m.get(field[6])).append("'");
	            }
				result.append(">").append(img).append(m.get(field[0])).append("</span>").append(checkbox.toString()).append(makeTree(table, m.get(field[1]).toString(), field, isOpen, hasCb, cbName, cbClick, condition)).append("</li>");
			}
			result.append("</ul>");
		}
		return result.toString();
	}
	
	
	/**
	 * <p>
	 * 说明:根据code获得第一级子节点列表
	 * </p>
	 * @param allNode 所有节点列表
	 * @param id 父节点id值
	 * @param condition
	 * @return
	 */
	private List<Map<String, Object>> getFirstLevelTree(String table, String id, String[] field, String condition)
	{
		String sort = field[5]; //排序字段
		if(field[5] == null || "".equals(field[5]))
		{
			sort = field[1];
		}
		StringBuffer sql = new StringBuffer();
		sql.append("select * from ").append(table).append(" where ");
		if(id == null || "".equals(id))
	    {
		    sql.append(field[2]).append(" is null ");
	    }
		else
		{
		    sql.append(field[2]).append(" = '").append(id).append("' ");
		}
		if(condition != null && !"".equals(condition))
		{
			sql.append(condition);
		}
		sql.append(" order by ").append(field[2]).append(", ").append(sort);
		
		List<Map<String, Object>> list = this.getSjt().queryForList(sql.toString());
		return list;
	}
	
}
